/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 *
 * @format
 * @flow strict-local
 */

import React, { Component } from "react";
import { NavigationContainer } from '@react-navigation/native';
import Storage from './src/util/storage_util';
import SplashScreen from 'react-native-splash-screen'
import user from './src/common/user'
import {
  StyleSheet,
  DeviceEventEmitter
} from 'react-native';
import { loginStack, routerStack } from "./src/router";

const navigationRef = React.createRef();
// 全局跳转
export function navigate(name, params) {
  navigationRef.current && navigationRef.current.navigate(name, params);
}

// 初始化操作
class App extends Component {
  state = {initName: 'Login',isSignIn:false}

  componentDidMount() {
    DeviceEventEmitter.addListener('login',(data)=>{
      this.setState({isSignIn:data.isLogin})
    })
    setTimeout(()=>{
      SplashScreen.hide()
    }, 1000, )
  }

  componentWillMount(){
    this.jump()
  }

  jump(){
      Storage.load({key:'token',autoSync: true}).then((value)=>{
        user.setUser({token:value.token})
        this.setState({isSignIn:value.token?true:false})
      }).catch((error)=>{
        this.setState({isSignIn:false})
      })
  }

  render() {
    return (
      this.state.isSignIn?(<NavigationContainer ref={navigationRef} >
        {routerStack()}
      </NavigationContainer>):(
        <NavigationContainer ref={navigationRef} >
          {loginStack()}
        </NavigationContainer>
      )
    )
  }
}

const styles = StyleSheet.create({

});

export default App;
